﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;


namespace testDraw
{
   
    /**
     * 类名称：FullScreenHelper
     * 用途：  用来设置和退出全屏
     * 用法:   直接调用
     *         FullScreenHelper.GoFullScreen(Window window)     进入全屏
     *         FullScreenHelper.ExitFullScreen(Window window)   退出全屏
     * 作者：  wanran
     * Email:  shan0xiao0xi@163.com
     * 时间:   2012-2-25
     */
    public static class FullScreenHelper
    {
        private static Window _FullWindow;
        private static WindowState _WindowState;
        private static WindowStyle _WindowStyle;
        private static bool _WindowTopMost;
        private static ResizeMode _WindowResizeMode;
        private static Rect _WindowRect;

     
        /************************************************************************/
        /* 函数名： GoFullScreen
         * 功能:    进入全屏模式
         * 参数:    当前窗口对象(Window)
         * 返回值:  null
        /************************************************************************/
        public static void GoFullScreen(this Window window)
        {
            //已经是全屏
            if (window.IsFullScreen()) return;

            //存储窗体信息
            _WindowState = window.WindowState;          //记录原窗口状态(还原，最大化，最小化)
            _WindowStyle = window.WindowStyle;          //记录原窗口边框样式
            _WindowTopMost = window.Topmost;            //记录原窗口是否在Z字形排列的最顶层(bool)
            _WindowResizeMode = window.ResizeMode;      //记录原窗口大小调整模式
            _WindowRect.X = window.Left;                //记录原窗口左边缘相对于桌面的位置
            _WindowRect.Y = window.Top;                 //记录原窗口上边缘相对于桌面的位置
            _WindowRect.Width = window.Width;           //记录原窗口的宽度
            _WindowRect.Height = window.Height;         //记录原窗口的高度


            //变成无边窗体
            window.WindowState = WindowState.Normal;    //假如已经是Maximized，就不能进入全屏，所以这里先调整状态
            window.WindowStyle = WindowStyle.None;
            window.ResizeMode = ResizeMode.NoResize;
            window.Topmost = true;                      //最大化后总是在最上面

            //获取窗口句柄 
            var handle = new WindowInteropHelper(window).Handle;

            //获取当前显示器屏幕
            Screen screen = Screen.FromHandle(handle);

            //调整窗口最大化,全屏的关键代码就是下面3句
            window.MaxWidth = screen.Bounds.Width;
            window.MaxHeight = screen.Bounds.Height;
            window.WindowState = WindowState.Maximized;


            //解决切换应用程序的问题
            window.Activated += new EventHandler(window_Activated);
            window.Deactivated += new EventHandler(window_Deactivated);

            //记住成功最大化的窗体
            _FullWindow = window;
        }

        static void window_Deactivated(object sender, EventArgs e)
        {
            var window = sender as Window;
            window.Topmost = false;
        }

        static void window_Activated(object sender, EventArgs e)
        {
            var window = sender as Window;
            window.Topmost = true;
        }

       
        /************************************************************************/
        /* 函数名:     ExitFullScreen
         * 功能:       退出全屏
         * 参数:       窗口对象Window
         * 返回值:     null
        /************************************************************************/
        public static void ExitFullScreen(this Window window)
        {
            //已经不是全屏无操作
            if (!window.IsFullScreen()) return;

            //恢复窗口先前信息，这样就退出了全屏
            window.Topmost = _WindowTopMost;
            window.WindowStyle = _WindowStyle;

            window.ResizeMode = ResizeMode.CanResize;//设置为可调整窗体大小
            window.Left = _WindowRect.Left;
            window.Width = _WindowRect.Width;
            window.Top = _WindowRect.Top;
            window.Height = _WindowRect.Height;

            window.WindowState = _WindowState;//恢复窗口状态信息

            window.ResizeMode = _WindowResizeMode;//恢复窗口可调整信息

            //移除不需要的事件
            window.Activated -= window_Activated;
            window.Deactivated -= window_Deactivated;

            _FullWindow = null;
        }

     
        /************************************************************************/
        /* 函数名:     IsFullScreen
         * 功能:       验证当前窗口是否为全屏状态
         * 参数:       窗口对象Window
         * 返回值:     bool
        /************************************************************************/
        public static bool IsFullScreen(this Window window)
        {
            if (window == null)
            {
                throw new ArgumentNullException("window");
            }
            return _FullWindow == window;
        }
    }
}
